package com.cqc.listviewindex01;

import android.content.Context;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Arrays;

import opensource.jpinyin.PinyinFormat;
import opensource.jpinyin.PinyinHelper;

public class MainActivity extends AppCompatActivity {

    private String[] names = {"宋江", "卢俊义", "吴用", "时迁", "段景柱", "施恩",
            "公孙胜", "关胜", "林冲", "秦明", "呼延灼", "花荣", "柴进", "李应", "朱仝", "鲁智深",
            "武松", "董平", "张清", "杨志", "徐宁", "索超", "戴宗", "刘唐", "李逵", "史进", "穆弘",
            "雷横", "李俊", "阮小二", "张横", "阮小五", " 张顺", "阮小七", "杨雄", "石秀", "解珍",
            " 解宝", "燕青", "朱武", "黄信", "孙立", "宣赞", "郝思文", "韩滔", "彭玘", "单廷珪",
            "魏定国", "萧让", "裴宣", "欧鹏", "邓飞", " 燕顺", "杨林", "凌振", "蒋敬", "吕方",
            "郭 盛", "安道全", "皇甫端", "王英", "扈三娘", "鲍旭", "樊瑞", "孔明", "孔亮", "项充",
            "李衮", "金大坚", "马麟", "童威", "童猛", "孟康", "侯健", "陈达", "杨春", "郑天寿",
            "陶宗旺", "宋清", "乐和", "龚旺", "丁得孙", "穆春", "曹正", "宋万", "杜迁", "薛永",
            "周通", "李忠", "杜兴", "汤隆", "邹渊", "邹润", "朱富", "朱贵", "蔡福", "蔡庆", "李立",
            "李云", "焦挺", "石勇", "孙新", "顾大嫂", "张青", "孙二娘", " 王定六", "郁保四", "白胜"};
    private ListView listView;
    private TextView tv_pin_yin;
    private Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_pin_yin = (TextView) findViewById(R.id.tv_pin_yin);
        listView = (ListView) findViewById(R.id.listView);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, Arrays.asList(names));
        listView.setAdapter(adapter);

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:

                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_FLING:

                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
//                        if (tv_pin_yin.getVisibility() == View.VISIBLE) {
//                            tv_pin_yin.setVisibility(View.GONE);
//                        }
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                String s = PinyinHelper.convertToPinyinString(names[firstVisibleItem], "", PinyinFormat.WITHOUT_TONE);
                String substring = s.substring(0, 1).toUpperCase();

                //方式一：以setVisibility(...)方式显示
                if (tv_pin_yin.getVisibility() != View.VISIBLE) {
                    tv_pin_yin.setVisibility(View.VISIBLE);
                }
                tv_pin_yin.setText(substring);
                //隐藏tv_pin_yin有2种方法，一是用Handler延迟1s隐藏，二是判断滑动状态，在SCROLL_STATE_IDLE(手指离开屏幕)时隐藏
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (tv_pin_yin.getVisibility() == View.VISIBLE) {
                            tv_pin_yin.setVisibility(View.GONE);
                        }
                    }
                }, 1000);


                //方式二：以Toast的方式显示
//                ToastUtil.showShortToast(context, substring);
            }
        });
    }
}
